#include "UserProfileHeader.h"

UserProfileHeader::UserProfileHeader(QWidget *parent)
    : QWidget{parent}
{
    QHBoxLayout *layout = new QHBoxLayout(this);

    // 设置圆形头像
    avatar = new AvatarLabel(this);
    avatar->setFixedSize(120, 120);
    layout->addWidget(avatar);

    avatar->setPixmap(QPixmap(":/test/resoures/test/avatar1.png"));

    // 用户信息
    QVBoxLayout *infoLayout = new QVBoxLayout();

    // 第一行：用户名和编辑按钮
    QHBoxLayout *topInfoLayout = new QHBoxLayout();
    username = new QLabel("脸滚键盘1201", this);
    username->setFont(QFont("Arial", 24, QFont::Bold)); // 设置字体大小为24，加粗显示
    safeButton = new QPushButton("🔒 账户安全", this);
    editButton = new QPushButton("⚙ 编辑资料", this);
    logoutButton = new QPushButton("⛔ 退出登录", this);

    // 设置按钮样式
    QString buttonStyle = R"(
        QPushButton {
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            border-radius: 15px;
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
    )";
    safeButton->setFixedSize(110, 30);
    safeButton->setStyleSheet(buttonStyle);
    editButton->setFixedSize(110, 30);
    editButton->setStyleSheet(buttonStyle);
    logoutButton->setFixedSize(110, 30);
    logoutButton->setStyleSheet(buttonStyle);

    topInfoLayout->addWidget(username);
    topInfoLayout->addStretch();
    topInfoLayout->addWidget(safeButton);
    topInfoLayout->addWidget(editButton);
    topInfoLayout->addWidget(logoutButton);
    infoLayout->addLayout(topInfoLayout);

    // 第二行：访问量、文章数、粉丝量
    QHBoxLayout *midInfoLayout = new QHBoxLayout();
    visitCount = new QLabel(this);
    articleCount = new QLabel(this);
    followerCount = new QLabel(this);
    // 示例：设置访问量标签的数字部分为加粗和更大字体
    visitCount->setText("<b><font size='5'>1</font></b> 访问量");
    articleCount->setText("<b><font size='5'>1</font></b> 文章");
    followerCount->setText("<b><font size='5'>0</font></b> 粉丝");
    midInfoLayout->addWidget(visitCount);
    midInfoLayout->addSpacing(20); // 间距
    midInfoLayout->addWidget(articleCount);
    midInfoLayout->addSpacing(20); // 间距
    midInfoLayout->addWidget(followerCount);
    midInfoLayout->addStretch(); // 添加弹性空间以确保靠左对齐
    infoLayout->addLayout(midInfoLayout);

    // 第三行：入站时间
    QHBoxLayout *bottomInfoLayout = new QHBoxLayout();
    joinDate = new QLabel("入站时间：2025-07-07", this);
    bottomInfoLayout->addWidget(joinDate);
    infoLayout->addLayout(bottomInfoLayout);

    // 个性签名
    signature = new QLabel("个性签名：这个人很懒，什么也没有留下。", this);
    infoLayout->addWidget(signature);

    layout->addLayout(infoLayout);
    setLayout(layout);

    connect(logoutButton, &QPushButton::clicked, this, &UserProfileHeader::logoutSignal);
    connect(editButton, &QPushButton::clicked, this, &UserProfileHeader::onEditButtonClicked);
    connect(safeButton, &QPushButton::clicked, this, &UserProfileHeader::onSafeButtonClicked);
}

void UserProfileHeader::onEditButtonClicked()
{
    EditProfileDialog dialog(this);

    connect(&dialog, &EditProfileDialog::profileUpdated, [this](const QPixmap &avatar, const QString &username, const QString &signature)
            {
                this->avatar->setPixmap(avatar);                    // 更新头像
                this->username->setText(username);                  // 更新用户名
                this->signature->setText("个性签名：" + signature); // 更新个性签名
            });

    dialog.exec();
}

void UserProfileHeader::onSafeButtonClicked()
{
    EditSafeDialog dialog(this);

    connect(&dialog, &EditSafeDialog::safeUpdated, [this](const QString &originalPassword, const QString &newPassword)
            { qDebug() << "原始密码：" << originalPassword << " 新密码：" << newPassword; });

    dialog.exec();
}
